import axios from 'axios';
import { API_ROOT } from '../config.jsx';

export const FETCH_HOME_DATA = 'FETCH_HOME_DATA';
export const CHANNEL_LOADMORE = 'CHANNEL_LOADMORE';
export const SWITCH_CHANNEL = 'SWITCH_CHANNEL';
export const INIT_ARTICLES = 'INIT_ARTICLES';
export const INIT_HOME_ARTICLES = 'INIT_HOME_ARTICLES';

export function fetchHomeData() {
    axios.defaults.headers.common['Authorization'] = sessionStorage.token;
    const info = axios.get(`${API_ROOT}/wechatapi/home`);
    return {
        type: FETCH_HOME_DATA,
        payload: info
    };
}

export function switchChannel(channelId, page, sort) {
    axios.defaults.headers.common['Authorization'] = sessionStorage.token;
    const articles = axios.get(`${API_ROOT}/wechatapi/Articles/${channelId}/${page}/${sort}`);
    return {
        type: SWITCH_CHANNEL,
        payload: articles
    }
}

export function loadMoreArticle(channelId, page, sort) {
    axios.defaults.headers.common['Authorization'] = sessionStorage.token;
    const articles = axios.get(`${API_ROOT}/wechatapi/Articles/${channelId}/${page}/${sort}`);
    return {
        type: CHANNEL_LOADMORE,
        payload: articles
    }
}

export function initArticles() {
    return {
        type: INIT_ARTICLES,
        payload: undefined
    }
}

export function initHomeArticles(articles, page, pageCount){
    
    return {
        type: INIT_HOME_ARTICLES,
        payload: {
            articles: articles,
            page: page,
            pageCount: pageCount
        }
    }
}